package cn.copera.dyy.common.utils.filter.builder;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Utility {
	private static SimpleDateFormat f = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");

	public static String getQueryDate(String start, String end, String name) {
		StringBuffer sb = new StringBuffer();
		sb.append("to_char(" + name + ", 'yyyy-mm-dd hh24:mi:ss')>='" + start
				+ " '");
		sb.append(" and to_char(" + name + ", 'yyyy-mm-dd hh24:mi:ss')<'" + end
				+ " ' )");
		return sb.toString();
	}

	public static String getQueryDate(String start, String name) {
		StringBuffer sb = new StringBuffer();
		sb.append("to_char(" + name + ", 'yyyy-mm-dd hh24:mi:ss')>='" + start
				+ " ')");

		return sb.toString();
	}
	public static Date getMonthTempDate(int num, Date d) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(d);

		cal.add(Calendar.DAY_OF_MONTH, num);
		return cal.getTime();

	}
	public static String getDataArea(String start, String end, String name)throws Exception {
		if ((start == null || "".equals(start.trim()))
				&& (end == null || "".equals(end.trim())))
			return null;
		try {
			Date ds = f.parse(start);
			Date de = f.parse(end);

			if (ds.after(de)) {
				String tmp = start;
				start = end;
				end = tmp;
			}
		} catch (Exception ex) {
		}
		//SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		//String endDate = sf.format(getMonthTempDate(1,sf.parse(end)));
		StringBuffer sb = new StringBuffer();
		if (start != null && !"".equals(start.trim()) && end != null
				&& !"".equals(end.trim())) {

			sb.append("(");
			sb.append(name + " BETWEEN '" + start+"'");
			sb.append(" AND '"+end+"')");

		} else if (start != null && !"".equals(start.trim())) {

			sb.append("(");
			sb.append(name + ">='" + start+"')");
		} else if (end != null && !"".equals(end.trim())) {
			sb.append("(");
			sb.append(name+"<='"+end+"')");
		}
		return sb.toString();
	}
	public static String getDateArea(String start, String end, String name)throws Exception {
		if ((start == null || "".equals(start.trim()))
				&& (end == null || "".equals(end.trim())))
			return null;
		try {
			Date ds = f.parse(start);
			Date de = f.parse(end);

			if (ds.after(de)) {
				String tmp = start;
				start = end;
				end = tmp;
			}
		} catch (Exception ex) {
		}
		//SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		//String endDate = sf.format(getMonthTempDate(1,sf.parse(end)));
		StringBuffer sb = new StringBuffer();
		if (start != null && !"".equals(start.trim()) && end != null
				&& !"".equals(end.trim())) {

			sb.append("(");
			sb.append(name + " BETWEEN '" + start+" 00:00:00'");
			sb.append(" AND '"+end+" 23:59:59')");

		} else if (start != null && !"".equals(start.trim())) {

			sb.append("(");
			sb.append(name + ">='" + start+"')");
		} else if (end != null && !"".equals(end.trim())) {
			sb.append("(");
			sb.append(name+"<='"+end+"')");
		}
		return sb.toString();
	}
	public static String getFieldArea(String start, String end, String name)throws Exception {
		if ((start == null || "".equals(start.trim()))
				&& (end == null || "".equals(end.trim())))
			return null;
		
		
		StringBuffer sb = new StringBuffer();
		if (start != null && !"".equals(start.trim()) && end != null
				&& !"".equals(end.trim())) {

			sb.append("(");
			sb.append(name + " BETWEEN " + Double.parseDouble(start));
			sb.append(" AND "+ Double.parseDouble(end)+")");

		} else if (start != null && !"".equals(start.trim())) {

			sb.append("(");
			sb.append(name + ">=" + Double.parseDouble(start)+")");
		} else if (end != null && !"".equals(end.trim())) {
			sb.append("(");
			sb.append(name+"<="+Double.parseDouble(end)+")");
		}
		return sb.toString();
	}
	public static String getMySQLQueryDataArea(String start, String end, String name) {
		if ((start == null || "".equals(start.trim()))
				&& (end == null || "".equals(end.trim())))
			return null;

		if (start != null && !"".equals(start.trim()) && end != null
				&& !"".equals(end.trim())) {
			try {

				String[] starts = start.split("-");
				String sm = starts[1];
				String sd = starts[2];
				String[] ends = end.split("-");
				String em = ends[1];
				String ed = ends[2];
				start = sm + sd + "000000";
				end = em + ed + "246060";
			} catch (Exception ex) {
			}
			return new StringBuffer("(").append(name).append(">='").append(
					start).append("' AND ").append(name).append("<'").append(
					end).append("')").toString();
		}

		if (start != null && !"".equals(start.trim()))
			return new StringBuffer(name).append(">='").append(start).append(
					"'").toString();

		return new StringBuffer(name).append("<'").append(end).append("'")
				.toString();
	}

	public static String nextDate(String d) {
		try {
			Calendar c = Calendar.getInstance();
			c.setTime(f.parse(d));
			c.add(Calendar.DAY_OF_YEAR, 1);
			return f.format(c.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) throws Exception {

		// System.out.print(Utility.getStateQuerySql(null, "2", null));
	}

	public static void inputStreamToOutputStream(InputStream in,
			OutputStream out) throws IOException {
		inputStreamToOutputStream(in, out, 1024);
	}

	public static void inputStreamToOutputStream(InputStream in,
			OutputStream out, int bufferSize) throws IOException {
		byte[] buffer = new byte[bufferSize];

		while (in.read(buffer) != -1) {
			out.write(buffer);
		}
	}

}
